package com.synergy.stockflow.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.constant.ResultCode;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.product.entity.ProcessDetail;
import com.synergy.stockflow.product.entity.ProcessName;
import com.synergy.stockflow.product.service.ProcessDetailService;
import com.synergy.stockflow.sales.entity.OrderReturns;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class ProcessDetailController {
    @Autowired
    private ProcessDetailService processDetailService;

    @GetMapping("/index")
    public Result<Page<ProcessDetail>> page(@RequestParam(name = "current", defaultValue = "1") int current,
                                            @RequestParam(name = "size", defaultValue = "5") int size,
                                            String processRouteId
    ){
        {
            QueryWrapper<ProcessDetail> query =new QueryWrapper<>();
            query.lambda().eq(ProcessDetail::getProcessRouteId,processRouteId);
            Page<ProcessDetail> page = new Page<>(current,size);
            page = processDetailService.page(page,query);
            return Result.success(page);
        }
    }
    @GetMapping("/Name")
    public Result<Page<ProcessName>>getName(String detailId){
        Page<ProcessName> page = new Page<>(1, 10000);
        page=processDetailService.Name(page,detailId);
        return Result.success(page);
    }
    @PostMapping("insert")
    public Result insert(@RequestBody ProcessDetail processDetail){
        QueryWrapper<ProcessDetail> query = new QueryWrapper<>();
        if (processDetail.getProcessRouteId() != null && processDetail.getProcessRouteId().length() > 0) {
            query.lambda().eq(ProcessDetail::getProcessRouteId, processDetail.getProcessRouteId());
        }
        if (processDetail.getSort() != null && processDetail.getSort() > 0) {
            query.lambda().eq(ProcessDetail::getSort, processDetail.getSort());
        }
        Page<ProcessDetail> page = new Page<>(1, 10000);
        Page<ProcessDetail> page1 = processDetailService.page(page, query);
        if (page1 == null || page1.getRecords().size() == 0) {

            processDetailService.save(processDetail);
            return Result.success(processDetail);
        }
        return Result.error(ResultCode.BUSINESS_EXCEPTION.value(),"序号已存在");

    }
    @PutMapping("update")
    public Result update(@RequestBody ProcessDetail processDetail){
        System.out.println(processDetail);
        processDetailService.updateById(processDetail);
        return Result.success(processDetail);
    }
    @DeleteMapping("/delete")
    public Result delete(@RequestBody List<ProcessName> processDetail){

        for (ProcessName detail:processDetail){
        }

        for(ProcessName detail:processDetail){
            processDetailService.removeById(detail.getDetailId());
        }
        return Result.success(processDetail);
    }
}



